﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QunYing.Common;
using System.IO;
using System.ServiceModel;
using QunYing.Model;

namespace Client
{
    public partial class FormWarnOrderAdd : BasePopForm
    {
        QunYing.BLL.VipInfo bll_vipInfo = new QunYing.BLL.VipInfo();
        QunYing.BLL.ServiceOrder bll_so = new QunYing.BLL.ServiceOrder();
        QunYing.Model.DeviceRealTimeInfo drti;
        public FormWarnOrderAdd()
        {
            InitializeComponent();
        }

        public void BindData(QunYing.Model.DeviceRealTimeInfo drti)
        {
            try
            {
                this.drti = drti;
                this.txt_报警信息.Text = drti.告警信息描述;
                this.txt_服务单号.Text = drti.数据发送时间.ToString("yyMMddHHmmss") + drti.发送号码.Substring(drti.发送号码.Length - 5, 4);
                this.txt_会员电话.Text = drti.用户号码;
                QunYing.Model.VipInfo vip = this.bll_vipInfo.GetModelList(string.Format("移动电话='{0}'", drti.用户号码))[0];
                this.txt_会员姓名.Text = vip.用户姓名;
                this.txt_通信地址.Text = vip.通信地址;
                this.txt_经销商ID.Text = vip.经销商编号;
                this.txt_会员卡号.Text = vip.会员卡号;
                this.txt_设备编号.Text = drti.设备ID;
                IImageOperator opera = ClientProxyFactory.GetProxy<IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationVip);
                Bitmap[] images = opera.GetImages(this.txt_经销商ID.Text, new string[] { vip.会员卡号 + ".jpg" });
                if (images.Length != 0)
                    this.pet_地址图片.Image = images[0];
                (opera as ICommunicationObject).Close();
            }
            catch (Exception ex)
            {
                QunYing.Common.Loger.Log.Error("新增出警单，绑定数据时出现异常：" + ex.Message);
            }

        }

        private void sharpButton3_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    Bitmap image = new Bitmap(this.panelControl1.Width, this.panelControl1.Height);
            //    Graphics grag = Graphics.FromImage(image);
            //    grag.CopyFromScreen(this.PointToScreen(this.panelControl1.Location), new Point(0, 0), this.panelControl1.Size);
            //    PrintService ps = new PrintService();
            //    MemoryStream ms = new MemoryStream();
            //    image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            //    grag.Dispose();
            //    image.Dispose();
            //    ps.StartPrint(ms, "image");
            //    ms.Dispose();
            //}
            //catch (Exception ex)
            //{
            //    QunYing.Common.Loger.Log.Error("打印出警单：" + ex.Message);
            //}

            if (drti != null)
            {
                System.Drawing.Image image = pet_地址图片.Image;
                QunYing.Model.ServiceOrder order = new ServiceOrder();
                order.报警信息编号 = drti.ID;
                order.出警方式 = this.ceb_出警方式.Text;
                order.出勤时间 = DateTime.Today;
                order.处理方式 = txt_处理方式.Text;
                order.处理结果 = cbe_处理结果.Text;
                order.服务单号 = this.txt_服务单号.Text;
                order.会员卡号 = this.txt_会员卡号.Text;
                order.技工联系电话 = this.txt_技工电话.Text;
                order.技工姓名 = this.txt_技工姓名.Text;
                order.经销商ID = this.txt_经销商ID.Text;
                order.设备编号 = this.txt_设备编号.Text;

                WarnOrderReport report = new WarnOrderReport(drti, order, image);
                report.ShowPreviewDialog();
            }
        }

        private void sharpButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void sharpButton1_Click(object sender, EventArgs e)
        {
            QunYing.Model.ServiceOrder model = new QunYing.Model.ServiceOrder();
            model.报警信息编号 = drti.ID;
            model.出警方式 = this.ceb_出警方式.Text;
            model.出勤时间 = DateTime.Today;
            model.处理方式 = txt_处理方式.Text;
            model.处理结果 = cbe_处理结果.Text;
            model.服务单号 = this.txt_服务单号.Text;
            model.会员卡号 = this.txt_会员卡号.Text;
            model.技工联系电话 = this.txt_技工电话.Text;
            model.技工姓名 = this.txt_技工姓名.Text;
            model.经销商ID = this.txt_经销商ID.Text;
            model.设备编号 = this.txt_设备编号.Text;
            try
            {
                int temp = bll_so.Add(model);
                if (temp != 0)
                {
                    drti.告警处理单据ID = temp;
                    drti.告警处理状态 = cbe_处理结果.Text;
                    if (cbe_处理结果.Text == "处理成功")
                    {
                        drti.告警解除时间 = DateTime.Now;
                        drti.告警是否解除 = true;
                    }

                    QunYing.BLL.DeviceRealTimeInfo bll_drti = new QunYing.BLL.DeviceRealTimeInfo();
                    bll_drti.Update(drti);
                    GlobalInfo.MsgBoxShowInfo("添加出警单成功！");
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                QunYing.Common.Loger.Log.Error("新增出警单，保存至数据库时出现了异常：" + ex.Message);
            }
        }


    }
}